﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

using MINERP.BLL.CustomerManage;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;

namespace MINERP.CustomerManage.Controller
{
    public class TalkRecordController : ControllerBase
    {

        #region //private fields

        private TalkRecordsManageBLL            _bll = new TalkRecordsManageBLL();
        private GridControl                     _grid = null;
        private GridView                        _view = null;
        private RepositoryItemGridLookUpEdit    _content = null;

        private OrderBagEntity                  _focusedBagObj = null;

        #endregion

        #region //Public Priorites

        public GridControl Grid
        {
            set
            {
                this._grid = value;
            }
        }


        public GridView View
        {
            set
            {
                this._view = value;
            }
        }

        public RepositoryItemGridLookUpEdit Content
        {
            set
            {
                this._content = value;
            }
        }

        #endregion


        #region //Internal Methods

        internal void GetTalkRecordListByOrderBag(object sender)
        {
            GridView view = sender as GridView;

            _focusedBagObj = view.GetFocusedRow() as OrderBagEntity;

            if (_focusedBagObj != null)
            {
                List<TalkRecordEntity> talkList = _bll.GetTalkRecordByOrderBag(_focusedBagObj.OrderBagID);
                this._grid.DataSource = talkList;

                //List<content> contents = new List<content>();

                //foreach (var talk in talkList)
                //{
                //    content item = new content();

                //    item.contents = talk.Context;
                //    item.id = talk.Id;

                //    contents.Add(item);
                //}

                //this._content.DataSource = contents;
            }
        }

        /// <summary>
        /// 设置指定订单的用户沟通信息
        /// </summary>
        /// <param name="_focusedBagObj"></param>
        public void SetTalkRecordListByOrderBag(OrderBagEntity _focusedBagObj)
        {
            try
            {
                if (_focusedBagObj != null)
                {
                    List<TalkRecordEntity> talkList = _bll.GetTalkRecordByOrderBag(_focusedBagObj.OrderBagID);
                    this._grid.DataSource = talkList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #region //Add Methods

        public void AddRecord(int userID, string content)
        {
            _bll.AddOneRecord(userID, content, _focusedBagObj);

            this._grid.DataSource = _bll.GetTalkRecordByOrderBag(_focusedBagObj.OrderBagID);
        }

        #endregion


        #region //check input
        internal bool Check(string text)
        {
            if (string.Compare(text, string.Empty) == 0)
            {
                MessageBox.Show("添加内容不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return false;
            }
            else
            {
                if (MessageBox.Show("确认添加记录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return false;
                }
            }

            return true;
        }

        #endregion


        #endregion

        /// <summary>
        /// 绑定内容
        /// </summary>
        public class content
        {
            public int id = 0;
            public string contents = string.Empty;
        }
    }
}
